//
//  HSYForgetPasswordViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/6.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYForgetPasswordViewController.h"
#import "NSString+Validation.h"
#import "HSYGetVerificationCodeRequest.h"
#import "HSYGetPasswordRequest.h"
#import "UIImage+Tools.h"

@interface HSYForgetPasswordViewController ()

@property (weak, nonatomic) IBOutlet UIButton *getIdentifyingCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *dispalyPWDBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *messageCodeTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *aNewPWDTextFiled;


@property(nonatomic,copy)NSString *code; //验证码

@property(nonatomic,assign)NSInteger second;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation HSYForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleButton setTitle:@"找回密码" forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
    
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

//获取验证码
- (IBAction)getMessageCodeBtnClick:(id)sender {
    
    if (![self.phoneNumberTextFiled.text isValidPhone]) {
        [self showMessage:@"请输入正确的手机号"];
        return;
    }
    
    self.getIdentifyingCodeBtn.enabled = NO;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateGetIdentifyingCodeBtnText) userInfo:nil repeats:YES];
    
    HSYGetVerificationCodeRequest *task = [[HSYGetVerificationCodeRequest alloc] init];
    task.cellphone = self.phoneNumberTextFiled.text;
    task.type = @"2";
    
    __weak typeof(self)weakSelf = self;
    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        if (response.resultCode != HSY_REQUEST_CODE_SUCCESS) {
            [weakSelf showMessage:response.resultMessage];
            [weakSelf.timer invalidate];
            weakSelf.timer = nil;
            
            weakSelf.getIdentifyingCodeBtn.enabled = YES;
            weakSelf.second = 0;
            [weakSelf.getIdentifyingCodeBtn setTitle:@"获得短信验证码" forState:UIControlStateNormal];
        }
    } failure:nil];
}

//更新重新获取验证码时间
-(void)updateGetIdentifyingCodeBtnText{
    NSString *time = [NSString stringWithFormat:@"%lds",(long)60- self.second];
    HSYLog(@"%@",time);
    
    [self.getIdentifyingCodeBtn setTitle:time forState:UIControlStateNormal];
    
    if (self.second == 60) {
        [self.timer invalidate];
        self.getIdentifyingCodeBtn.enabled = YES;
        self.second = 0;
        [self.getIdentifyingCodeBtn setTitle:@"重新获得验证码" forState:UIControlStateNormal];
        
        return;
    }
    
    self.second++;
}


- (IBAction)submitBtnClcik:(UIButton *)sender {

    if (![self.phoneNumberTextFiled.text isValidPhone]) {
        [self showMessage:@"请输入正确的手机号"];
        return;
    }
    
    if (!self.messageCodeTextFiled.text.length) {
        [self showMessage:@"请输入验证码"];
        
        return;
    }
    
    if (!self.aNewPWDTextFiled.text.length) {
        [self showMessage:@"新密码"];
        return;
    }
    
    if (self.aNewPWDTextFiled.text.length<6) {
        [self showMessage:@"请输入6位以上的密码"];
        return;
    }
    
    
    HSYGetPasswordRequest *task = [[HSYGetPasswordRequest alloc] init];
    task.cellphone = self.phoneNumberTextFiled.text;
    task.code = self.messageCodeTextFiled.text;
    task.password = self.aNewPWDTextFiled.text;
    
    __weak typeof(self)weakSelf = self;

    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        
        [weakSelf showMessage:response.resultMessage];
        
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1ull * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
    
}


- (IBAction)dispalyPWDBtnCLick:(id)sender {
    
    self.dispalyPWDBtn.selected = !self.dispalyPWDBtn.selected;
    
    if (self.dispalyPWDBtn.selected) {
        self.aNewPWDTextFiled.secureTextEntry = NO;
        
    }else{
        self.aNewPWDTextFiled.secureTextEntry = YES;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
